﻿var AdjBom = {};
//设置
AdjBom.settings = {
    TaskId: com.get.TaskId,
    TaskCode: "",
    editRow: undefined,
    ColumnsParam: [],
    currentTr: {},
    EquipId: 0
};

AdjBom.init = function () {

    $("#TaskId").val(AdjBom.settings.TaskId);
    var Xuhao = com.get.Xuhao;
    var OrgId = com.get.OrgId;
    //加载任务信息
    com.ajax({
        url: "/Logic/TM/TaskManagerAjax.aspx?method=GetTaskInfoByPrimaryId&TaskId=" + AdjBom.settings.TaskId, success: function (data) {
            $("#lblXuhao").html(Xuhao);
            $("#lblTaskCode").html(data.TaskCode);
            $("#lblProjName").html(data.ProjName);
            $("#lblEngName").html(data.EngName);
            $("#lblEquipName").html(data.EquipName);
            $("#lblEquipModel").html(data.EquipModel);
            $("#lblNum").html(data.TaskNum);
            $("#hidEquipId").val(data.EquipId);
            AdjBom.settings.TaskCode = data.TaskCode;
            AdjBom.settings.EquipId = data.EquipId;
            AdjBom.settings.EquipModel = data.EquipModel;
        }
    });

    var height = $("#divTotal").height();
    var master = $("#master").height();
    var content = parseFloat(height) - parseFloat(master) - 50;
    $("#tbList").css("height", content);




    //加载原始数据列表
    $("#tbList").treegrid({
        url: '/Logic/TM/OrgDataAjax.aspx?method=GetTreeOrgDataByTaskId',
        loadMsg: '数据加载中请稍候……',
        // fitColumns: true,
        queryParams: { TaskId: AdjBom.settings.TaskId,OrgId:OrgId },
        idField: "ID",
        treeField: "ID",
        singleSelect: false,
        autoRowHeight: true,
        nowrap: false,
        striped: true,
        frozenColumns: [[
             { field: 'ck', checkbox: true, width: 30 },
             {
                 field: 'ID', title: '序号', align: "left", halign: "center", width: 120, formatter: function (value, row, Index) {

                     return row.Xuhao;
                 }
             },
             { field: 'ComponentIDNum', title: '构件标识号', align: "center", width: 120 },
             { field: 'MaterialName', title: '名称', align: "center", width: 120 },
             {
                 field: 'MkState', title: '提交状态', align: "center", width: 100, formatter: function (value, row, Index) {
                     if (value == "0") {
                         return "未提交";
                     } else if (value == "1") {
                         return "已提交";
                     }
                 }
             },
             { field: 'ComponentNum', title: '零件号', align: "center", width: 60 },
             { field: 'SingleNum', title: '数量 ', align: "center", width: 40 },
             { field: 'MaterialGuige', title: '截面型材', align: "center", width: 100 },
             { field: 'MaterialCaizhi', title: '材质 ', align: "center", width: 100 }]],
            columns: [[
             { field: 'Length', title: '长度', align: "center", width: 60 },
             { field: 'UnitWeight', title: '单重', align: "center", width: 70 },
             { field: 'TotalWeight', title: '总重', align: "center", width: 70 },
             //{ field: 'MaterialGB', title: '国标', align: "center", width: 120 },
             //{ field: 'HidGuige', title: '材料规格', align: "center", width: 100 },
             { field: 'SingleArea', title: '单面积', align: "center", width: 60 },
             { field: 'Area', title: '总面积', align: "center", width: 60 },
             { field: 'TotalLength', title: '总长度', align: "center", width: 60 },
             { field: 'Width', title: '宽度 ', align: "center", width: 60 },
             { field: 'Height', title: '高度 ', align: "center", width: 60 },
             //{ field: 'MaterialTheroWeight', title: '理论重量', align: "center", width: 60 },
             { field: 'MaUnitWeight', title: '单重(毛)', align: "center", width: 70 },
             { field: 'MaTotalWeight', title: '总重(毛)', align: "center", width: 70 },
             { field: 'Kong', title: '孔', align: "center", width: 100 },
             { field: 'PackNum', title: '包号', align: "center", width: 60 },
             { field: 'PaintArea', title: '喷漆面积', align: "center", width: 60 },
             { field: 'Note', title: '备注', align: "center", width: 350 },
             { field: 'PlanArea', title: '计划面域', align: "center", width: 70 },
             { field: 'MaState', title: '毛胚状态 ', align: "center", width: 60 },
             { field: 'PaintProgram', title: '涂装方案', align: "center", width: 60 },
             {
                 field: 'Process', title: '工艺流程 ', align: "center", width: 70, formatter: function (value, row, Index) {

                     return com.getArrayText(StaticCombo.Process, value);
                 }
             },
             { field: 'IsFixedSize', title: '是否定尺 ', align: "center", width: 60 },
             { field: 'SelfWeight', title: '部件自重 ', align: "center", width: 60 },
             { field: 'IsSubmitMs', title: '制作明细 ', align: "center", width: 60 },
             { field: 'IsSubmitMp', title: '材料计划 ', align: "center", width: 60 },
             {
                 field: 'BlankType', title: '下料类别 ', align: "center", width: 70, formatter: function (value, row, Index) {

                     return com.getArrayText(StaticCombo.BlankType, value);
                 }
             },
               //{ field: 'FillDate', title: '操作时间 ', align: "center", width: 140, sortable: true },
             //{ field: 'ClassName', title: '制作班组 ', align: "center", width: 90 },
             { field: 'RePlaceGuige', title: '代用规格', align: "center", width: 100 },
             { field: 'RePlaceCaizhi', title: '代用材质', align: "center", width: 100 },


             //{ field: 'OrgMaterialName', title: '原始物料名称', align: "center", width: 80 }
       ]]
});


// 初始化父类Combo
$("#IsAdjustMapXuhao").combobox({
    data: StaticCombo.Pick,
    valueField: 'value',
    textField: 'text',
    panelHeight: 'auto',
    editable: false,
});


//初始化列显示隐藏界面
$('#divAdjust').dialog({
    title: '调整序号',
    width: 230,
    height: 150,
    collapsible: true,
    minimizable: false,
    closable: true,
    cache: false,
    buttons: [{
        text: "确定", icon: "icon-save", handler: function () {
            var rows = $("#tbList").treegrid("getSelections");
            var Xuhao = $("#comboParent").val();
            if (rows.length > 0 && rows.length < 2) {
                com.ajax({
                    url: "/Logic/TM/OrgDataAjax.aspx?method=ChangeOrgParent", data: { orgId: rows[0].ID, Xuhao: Xuhao, TaskId: AdjBom.settings.TaskId, IsAdjustMapXuhao: $("#IsAdjustMapXuhao").combobox("getValue") }, success: function (data) {
                        $("#tbList").treegrid("unselectAll");
                        $("#tbList").treegrid("load");

                        $("#divAdjust").dialog("close");
                        com.message(data.state, data.msg, function () {

                        });

                    }
                });
            }
            else {
                com.message("information", "请选择一条数据");
            }
        }
    }, { text: "取消", handler: function () { $("#divAdjust").dialog("close"); } }]
});
$("#divAdjust").dialog("close");


//初始化拆分窗口
$('#divDivide').dialog({
    title: '编辑窗口',
    width: 230,
    height: 150,
    collapsible: true,
    minimizable: false,
    closable: true,
    cache: false,
    buttons: [{
        text: "确定", icon: "icon-save", handler: function () {

            var rows = $("#tbList").treegrid("getSelections");
            var splitNum = $('#txtDivide').numberbox('getValue');
            if (splitNum == 0 || splitNum == "") {
                com.message("information", "拆分数量不能为空！");
                return;
            }
            if (rows.length > 0 && rows.length < 2) {
                com.ajax({
                    url: "/Logic/TM/OrgDataAjax.aspx?method=SplitOrg", data: { orgId: rows[0].ID, splitNum: splitNum, TaskId: AdjBom.settings.TaskId, Xuhao: $("#txtNewXuhao").val() }, success: function (data) {

                        $("#tbList").treegrid("load");
                        com.message(data.state, data.msg, function () {

                        });

                    }
                });

            } else {
                com.message("information", "请选择一条数据");
            }
        }
    }, { text: "取消", handler: function () { $("#divDivide").dialog("close"); } }]
});
$("#divDivide").dialog("close");


}



//编辑
AdjBom.EditOrgData = function () {
    var rows = $("#tbList").treegrid("getSelections");
    var orgIds = "";
    if (rows.length > 0) {
        for (var i = 0; i < rows.length; i++) {
            if (rows[i].MpReviewState == "2" || rows[i].MsReviewState == "2" || rows[i].OsReviewState == "2" || rows[i].MpReviewState == "1" || rows[i].MsReviewState == "1" || rows[i].OsReviewState == "1") {
                com.message("information", "该条记录正在审核中，无法编辑");
                return;
            }
            else {
                orgIds += rows[i].ID + ",";
            }
        }
        top.wrapper.addTab("原始数据编辑", "/UI/TM/OrgDetailEdit.aspx?action=edit&TaskId=" + AdjBom.settings.TaskId + "&orgIds=" + orgIds.trim(','));

    }
    else {
        com.message("information", "请选中一行");
    }
}

AdjBom.DeleteOrg = function () {
    var rows = $("#tbList").treegrid("getSelections");
    var Xuhao = "";
    if (rows.length > 0) {
        for (var i = 0; i < rows.length; i++) {
            Xuhao += rows[i].Xuhao + ","
        }
        Xuhao.trim(',');
        com.message("confirm", "您确定要删除明细序号为【" + Xuhao + "】的记录吗？", function (r) {
            if (r) {
                //  alert("a");
                com.ajax({
                    url: "/Logic/TM/OrgDataAjax.aspx?method=DeleteOrg", data: { TaskId: AdjBom.settings.TaskId, data: JSON.stringify(rows) }, success: function (data) {
                        com.message(data.state, data.msg, function () {

                            for (var i = 0; i < rows.length; i++) {
                                $("#tbList").treegrid("load");

                            }

                        })
                    }
                });
            }

        })
    }
    else {
        com.message("information", "请选中一行");
    }


}


AdjBom.AdjParent = function () {

    $("#divAdjust").dialog("open");

}

AdjBom.Divide = function () {

    $("#divDivide").dialog("open");

}

$(AdjBom.init);